﻿// ChuangGuan.cpp : 实现文件
//

#include "stdafx.h"
#include "TeacherManager.h"
#include "ChuangGuan.h"
#include "afxdialogex.h"
CString strAllNeiRong;

// CChuangGuan 对话框

IMPLEMENT_DYNAMIC(CChuangGuan, CDialogEx)

    CChuangGuan::CChuangGuan(CWnd* pParent /*=NULL*/)
    : CDialogEx(CChuangGuan::IDD, pParent)
    , m_nRadioId(0)
{
    m_flagOfKeNeiWai=true;
    m_IndexBegin=0;
    m_indexCurrent=1;
}

CChuangGuan::~CChuangGuan()
{
}

void CChuangGuan::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_RICHEDIT21, m_edit);
    DDX_Control(pDX, IDC_COMBO_CLASS, m_comboxNianji);
    DDX_Control(pDX, IDC_COMBO_CHU_BAN, m_comboxBanben);
    DDX_Control(pDX, IDC_COMBO_FEN_LEI, m_comboxDanyuan);
    DDX_Control(pDX, IDC_COMBO_KE_WEN_NAME, m_comboxKewen);
    DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
    DDX_Control(pDX, IDC_STATIC_KE_WEN_NAME, m_kewenName);
    DDX_Control(pDX, IDC_COMBO_CLASS_B, m_comboxKeWaiNianji);
    DDX_Control(pDX, IDC_COMBO_FEN_LEI_B, m_comboxKeWaiType);
    DDX_Control(pDX, IDC_COMBO_KE_WEN_NAME_B, m_comboxKeWaiWen);
    DDX_Control(pDX, IDC_BUTTON_KE_WEN, m_btnKeWen);
    DDX_Control(pDX, IDC_BUTTON_KE_WAI, m_btnKeWai);

    DDX_Control(pDX, IDC_RADIO_TEXTBOOK, m_radioChoiceText);
    DDX_Radio(pDX, IDC_RADIO_TEXTBOOK, m_nRadioId);
}


BEGIN_MESSAGE_MAP(CChuangGuan, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_KE_WEN, &CChuangGuan::OnBnClickedButtonKeWen)
    ON_BN_CLICKED(IDC_BUTTON_KE_WAI, &CChuangGuan::OnBnClickedButtonKeWai)
    ON_BN_CLICKED(IDC_BUTTON_BEGIN, &CChuangGuan::OnBnClickedButtonBegin)
    ON_BN_CLICKED(IDC_BUTTON_CHONG_XIN, &CChuangGuan::OnBnClickedButtonChongXin)
    ON_BN_CLICKED(IDC_BUTTON_NEXT, &CChuangGuan::OnBnClickedButtonNext)
    ON_NOTIFY(NM_CUSTOMDRAW, IDC_PROGRESS1, &CChuangGuan::OnNMCustomdrawProgress1)
    ON_WM_TIMER()
    //	ON_CBN_SELENDCANCEL(IDC_COMBO_CLASS, &CChuangGuan::OnCbnSelendcancelComboClass)
    ON_CBN_SELCHANGE(IDC_COMBO_CHU_BAN, &CChuangGuan::OnCbnSelchangeComboChuBan)
    ON_CBN_SELCHANGE(IDC_COMBO_FEN_LEI, &CChuangGuan::OnCbnSelchangeComboFenLei)
    ON_CBN_SELCHANGE(IDC_COMBO_KE_WEN_NAME, &CChuangGuan::OnCbnSelchangeComboKeWenName)
    ON_CBN_SELCHANGE(IDC_COMBO_CLASS, &CChuangGuan::OnCbnSelchangeComboClass)
    ON_CBN_SELCHANGE(IDC_COMBO_CLASS_B, &CChuangGuan::OnCbnSelchangeComboClassB)
    ON_CBN_SELCHANGE(IDC_COMBO_FEN_LEI_B, &CChuangGuan::OnCbnSelchangeComboFenLeiB)
    ON_BN_CLICKED(IDC_RADIO_TEXTBOOK, &CChuangGuan::OnClickedRadioChoiceText)
    ON_BN_CLICKED(IDC_RADIO_OUTSIDE_READING, &CChuangGuan::OnClickedRadioChoiceText)
    ON_CBN_SELCHANGE(IDC_COMBO_KE_WEN_NAME_B, &CChuangGuan::OnCbnSelchangeComboKeWenNameB)
END_MESSAGE_MAP()


// CChuangGuan 消息处理程序


BOOL CChuangGuan::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    GetDlgItem(IDC_BUTTON_CHONG_XIN)->ShowWindow(FALSE);
    GetDlgItem(IDC_BUTTON_NEXT)->ShowWindow(FALSE);
    // TODO:  在此添加额外的初始化
    InitCmb();
    m_btnKeWen.SetFaceColor(RGB(0,255,0));
    m_btnKeWai.SetFaceColor(RGB(220,220,220));
    m_Progress.ShowWindow(FALSE);
    m_Progress.ShowWindow(FALSE);
    ModifyStyleEx(WS_EX_TOOLWINDOW, WS_EX_APPWINDOW);
    HICON hIcon;  
    hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
    SetIcon(hIcon, TRUE);

    m_comboxKeWaiNianji.EnableWindow(FALSE);
    m_comboxKeWaiType.EnableWindow(FALSE);
    m_comboxKeWaiWen.EnableWindow(FALSE);



    /*SetIcon(hIcon, FALSE);*/

    //CString strContent(_T("hi,baby~"));
    //m_editFont.Detach();
    ///*m_editFont.CreatePointFont(iZiHao*10, strZiTi);*/
    //SetUnderline(TRUE);
    //
    //GetDlgItem(IDC_RICHEDIT21)->SetFont(&m_editFont); 
    //SetDlgItemText(IDC_RICHEDIT21, strContent.GetString());

    //long   nStart   =   0;   
    //long   nEnd   =   m_edit.GetTextLength();   

    //m_edit.SetSel(nStart,   nEnd);   
    //SetColor();
    //m_edit.SetSelectionCharFormat(m_cfDefault); 
    //UpdateData(FALSE);
    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}

//课文确定;
void CChuangGuan::OnBnClickedButtonKeWen()
{
    //清理工作，初始化
    m_comboxNianji.EnableWindow();
    m_comboxBanben.EnableWindow();
    m_comboxDanyuan.EnableWindow();
    m_comboxKewen.EnableWindow();

    m_comboxKeWaiNianji.EnableWindow(FALSE);
    m_comboxKeWaiType.EnableWindow(FALSE);
    m_comboxKeWaiWen.EnableWindow(FALSE);

    BeforeBtnClick();
    m_btnKeWen.SetFaceColor(RGB(0,255,0));
    m_btnKeWai.SetFaceColor(RGB(220,220,220));
    IniKeWenNeiRongCmb();
    m_flagOfKeNeiWai=true;
    m_IndexBegin=m_comboxKewen.GetCurSel();
}

//课外素材确定;
void CChuangGuan::OnBnClickedButtonKeWai()
{	
    //清理工作，初始化

    m_comboxNianji.EnableWindow(FALSE);
    m_comboxBanben.EnableWindow(FALSE);
    m_comboxDanyuan.EnableWindow(FALSE);
    m_comboxKewen.EnableWindow(FALSE);

    m_comboxKeWaiNianji.EnableWindow();
    m_comboxKeWaiType.EnableWindow();
    m_comboxKeWaiWen.EnableWindow();

    BeforeBtnClick();
    m_btnKeWen.SetFaceColor(RGB(220,220,220));
    m_btnKeWai.SetFaceColor(RGB(0,255,0));
    IniKeWaiKeWenNeiRongCmb();
    m_flagOfKeNeiWai = false;
    m_IndexBegin = m_comboxKeWaiWen.GetCurSel();
}

//btn清理，初始化，
void CChuangGuan::BeforeBtnClick()
{
    //清理，初始化工作
    KillTimer(TIMER_BEFORE_AUDIO_RECORD);
    KillTimer(TIMER_AUDIO_RECORD);
    KillTimer(TIMER_MARK_SENTENCE_PROGRESS);
    m_Progress.SetPos(0);
    SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, _T(""));
    //
}

//开始闯关;
void CChuangGuan::OnBnClickedButtonBegin()
{
    //清理工作，初始化
    BeforeBtnClick();
    GetDlgItem(IDC_BUTTON_BEGIN)->EnableWindow(FALSE);
    switch (m_nRadioId)
    {
    case 0:
        IniKeWenNeiRongCmb();
        break;
    case 1:
        IniKeWaiKeWenNeiRongCmb();
        break;
    default:
        break;
    }

    //if (!m_strCurrentReadInfo.IsEmpty())
    //{
    //	SetDlgItemText(IDC_RICHEDIT_ENGLISH_SENTENCE,m_strCurrentReadInfo);
    //}
    GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);
    AudioRecord();	
    GetDlgItem(IDC_BUTTON_CHONG_XIN)->ShowWindow(TRUE);
    GetDlgItem(IDC_BUTTON_NEXT)->ShowWindow(TRUE);
    GetDlgItem(IDC_BUTTON_BEGIN)->EnableWindow(TRUE);
}

//重新挑战;
void CChuangGuan::OnBnClickedButtonChongXin()
{
    //清理工作，初始化
    BeforeBtnClick();

    if (m_flagOfKeNeiWai)
    {
        //课内
        m_comboxKewen.SetCurSel(m_IndexBegin);
        IniKeWenNeiRongCmb();
    }
    else
    {
        //课外
        m_comboxKeWaiWen.SetCurSel(m_IndexBegin);
        IniKeWaiKeWenNeiRongCmb();
    }

    /*if (!m_strCurrentReadInfo.IsEmpty())
    {
    SetDlgItemText(IDC_RICHEDIT_ENGLISH_SENTENCE,m_strCurrentReadInfo);
    }*/

    GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);
    AudioRecord();
}

//下一关;
void CChuangGuan::OnBnClickedButtonNext()
{
    //清理工作，初始化
    BeforeBtnClick();
    //
    if (m_flagOfKeNeiWai)
    {
        //课内
        m_indexCurrent=m_comboxKewen.GetCurSel()+1;
        if (m_indexCurrent<m_comboxKewen.GetCount())
        {
            m_comboxKewen.SetCurSel(m_indexCurrent);
            IniKeWenNeiRongCmb();
            /*if (!m_strCurrentReadInfo.IsEmpty())
            {
            SetDlgItemText(IDC_RICHEDIT_ENGLISH_SENTENCE,m_strCurrentReadInfo);
            }*/
            GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);
            AudioRecord();
        }
        else
        {
            AfxMessageBox(_T("本单元闯关结束"));
        }			
    }
    else
    {
        //课外
        m_indexCurrent=m_comboxKeWaiWen.GetCurSel()+1;
        if (m_indexCurrent<m_comboxKeWaiWen.GetCount())
        {
            m_comboxKeWaiWen.SetCurSel(m_indexCurrent);
            IniKeWaiKeWenNeiRongCmb();
            /*if (!m_strCurrentReadInfo.IsEmpty())
            {
            SetDlgItemText(IDC_RICHEDIT_ENGLISH_SENTENCE,m_strCurrentReadInfo);
            }*/
            GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);
            AudioRecord();
        }
        else
        {
            AfxMessageBox(_T("本单元闯关结束"));
        }	
    }
}

//设置下滑线
void CChuangGuan::SetUnderline(BOOL bUnderline)
{
    LOGFONT lf;
    GetFont()->GetLogFont(&lf);
    lf.lfUnderline = bUnderline;
    lf.lfHeight=20;
    m_editFont.DeleteObject();
    BOOL B=m_editFont.CreateFontIndirect(&lf);
    SetFont(&m_editFont,FALSE);	
}
//设置字体颜色
void CChuangGuan::SetColor()
{
    memset(&m_cfDefault,   0,   sizeof(m_cfDefault));   

    m_cfDefault.cbSize   =   sizeof(CHARFORMAT);   

    m_cfDefault.dwMask   =   CFM_BOLD   |   CFM_COLOR   |   CFM_FACE   |   CFM_ITALIC   |   CFM_SIZE   |   CFM_STRIKEOUT  /* |   CFM_UNDERLINE*/;   

    m_cfDefault.crTextColor   =   RGB(255,   0,   0);//颜色   

    _tcscpy_s(m_cfDefault.szFaceName,   _T("宋体"));//字体   

    m_cfDefault.bPitchAndFamily   =   FF_SWISS;
    m_cfDefault.yHeight   =   260; 

}
void CChuangGuan::SetSizeOfFont()
{
    CHARFORMAT2 cf;
    ZeroMemory(&cf, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    cf.yHeight=260;
    m_edit.SetSel(-1,-1);
    m_edit.SetSelectionCharFormat(cf);
}
//初始化combox
void CChuangGuan::InitCmb()
{
    //课外
    InitKeWaiNianjiCmb();
    InitKeWaiTypeCmb();
    InitKeWaiWenCmb();

	    //课内
    InitNianjiCmb();
    InitBanBenCmb();
    InitDanYuanCmb();
    IniKeWenCmb();
    IniKeWenNeiRongCmb();
}
void CChuangGuan::InitNianjiCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    //选中项的索引
    int index;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文]

    //年级
    m_comboxNianji.ResetContent();
    Sql.Format (_T("SELECT   ID, nianji_name, nianji_id FROM  nyxm_k_nianji"));
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("nianji_name"));
        m_comboxNianji.AddString(Name);
        pRecdPtr->MoveNext();
    }
    index=m_comboxNianji.SelectString(0,_T("小学一年级"));
    m_comboxNianji.SetCurSel(index);
}
void CChuangGuan::InitBanBenCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文]
    CString ID;
    CString StrOfSelected;

    //banben
    m_comboxNianji.GetLBText(m_comboxNianji.GetCurSel(),StrOfSelected);
    Sql.Format(_T("SELECT ID FROM  nyxm_k_nianji where nianji_name=\"%s\""),StrOfSelected);
    ID=theApp.GetSingleFieldEx(Sql,false);
    if (ID.IsEmpty())
    {
        return;
    }
    m_comboxBanben.ResetContent();
    Sql.Format(_T("SELECT   ID, nianji_id, banben_name FROM  nyxm_k_banben where nianji_id=\"%s\""),ID);
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("banben_name"));
        m_comboxBanben.AddString(Name);
        pRecdPtr->MoveNext();
    }
    m_comboxBanben.SetCurSel(0);
}
void CChuangGuan::InitDanYuanCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文]
    CString ID;
    CString StrOfSelected;

    //单元
    m_comboxBanben.GetLBText(m_comboxBanben.GetCurSel(),StrOfSelected);
    Sql.Format(_T("SELECT ID FROM  nyxm_k_banben where banben_name=\"%s\""),StrOfSelected);
    ID=theApp.GetSingleFieldEx(Sql,false);
    if (ID.IsEmpty())
    {
        return;
    }
    m_comboxDanyuan.ResetContent();
    Sql.Format(_T("SELECT   ID, banben_id, danyuan_name FROM   nyxm_k_danyuan where banben_id=\"%s\""),ID);
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    int i = 0;
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("danyuan_name"));
        ID=GetCollect(pRecdPtr,_T("ID"));
        //因为单元名字有重复，所以显示为Name+ID
        //m_comboxDanyuan.AddString(Name+_T("+")+ID);
        //m_comboxDanyuan.AddString(Name);
        m_comboxDanyuan.InsertString(i, Name);
        m_comboxDanyuan.SetItemData(i,_tstoi(ID));
        i++;
        pRecdPtr->MoveNext();
    }
    m_comboxDanyuan.SetCurSel(0);
}
void CChuangGuan::IniKeWenCmb()
{	
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文]
    CString ID;
    CString StrOfSelected;

    //kewen
    //m_comboxDanyuan.GetLBText(m_comboxDanyuan.GetCurSel(),StrOfSelected);
    //因为导入数据库单元名称重复的问题,直接从单元combox截取ID
    //int pos=StrOfSelected.Find('+');
    //ID=StrOfSelected.Right(StrOfSelected.GetLength()-pos-1);
    //if (ID.IsEmpty())
    {
        //return;
    }
    //m_comboxKewen.ResetContent();

    int n = m_comboxDanyuan.GetItemData(m_comboxDanyuan.GetCurSel());
    ID.Format(_T("%d"),n);

    Sql.Format(_T("SELECT   ID, danyuan_id, danyuan_name, cont, mp3 FROM  nyxm_k_kewen where danyuan_id=\"%s\""),ID);
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    int i =0;
    m_comboxKewen.ResetContent();
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("danyuan_name"));
        ID=GetCollect(pRecdPtr,_T("ID"));
        //m_comboxKewen.AddString(Name+_T("+")+ID);
        m_comboxKewen.InsertString(i, Name);
        m_comboxKewen.SetItemData(i,_tstoi(ID));
        i++;
        pRecdPtr->MoveNext();
    }
    m_comboxKewen.SetCurSel(0);
}
void CChuangGuan::IniKeWenNeiRongCmb()
{
    //清空richedit
    //m_edit.SetSel(0,-1);
    //m_edit.Clear();
    strAllNeiRong.Format(_T(""));
    m_edit.SetWindowText(_T(""));
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString strOfFenju;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文->内容]
    CString ID;
    CString StrOfSelected;

    //内容IDC_RICHEDIT21，从分句中查询
    m_comboxKewen.GetLBText(m_comboxKewen.GetCurSel(),StrOfSelected);
    int pos=StrOfSelected.Find('+');
    //文章标题
    //CString strName=StrOfSelected.Left(pos);
    m_kewenName.SetWindowTextW(StrOfSelected);
    //ID=StrOfSelected.Right(StrOfSelected.GetLength()-pos-1);
    //if (ID.IsEmpty())
    {
        //return;
    }
    int n = m_comboxKewen.GetItemData(m_comboxKewen.GetCurSel());
    ID.Format(_T("%d"),n);
    Sql.Format(_T("SELECT   ID, kewen_id, fenju, juzi FROM  nyxm_k_fenju where kewen_id=\"%s\""),ID);
    pRecdPtr= theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }

    //SetColor();
    //m_edit.SetSelectionCharFormat(m_cfDefault); 
    SetUnderline(TRUE);
    GetDlgItem(IDC_RICHEDIT21)->SetFont(&m_editFont);
    /*SetSizeOfFont();*/
	m_iMaxProcessRange = 0;
    while(!pRecdPtr->GetadoEOF())
    {
        strOfFenju=GetCollect(pRecdPtr,_T("juzi"));
        strOfFenju.Remove('\r');
        strOfFenju.Remove('\n');
        strOfFenju.Remove('\t');
        m_edit.SetSel(-1, -1);

		for(int i = 0; i < strOfFenju.GetLength(); i++){
			if(strOfFenju[i] == '.' || strOfFenju[i] == '!' || strOfFenju[i] == '?'){
				m_iMaxProcessRange++;
			}
		}

        if ((-1) != strOfFenju.Find(_T(".")) || (-1) != strOfFenju.Find(_T("!")) ||(-1) != strOfFenju.Find(_T("?")))
        {
            /*strOfFenju = strOfFenju.Left(strOfFenju.GetLength() - 1);*/
            strOfFenju+=_T("\r");
        }	
        m_edit.ReplaceSel( (LPCTSTR)strOfFenju );
        CHARFORMAT cf = {0};
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_FACE|CFM_SIZE|CFM_BOLD;
        cf.cbSize = sizeof(CHARFORMAT);
        cf.yHeight=260;
        m_edit.SetDefaultCharFormat(cf);
        strAllNeiRong+=strOfFenju;
        pRecdPtr->MoveNext();
    }
	m_edit.SetSel(0,0);
}
void CChuangGuan::InitKeWaiNianjiCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    //年级
    m_comboxKeWaiNianji.ResetContent();
    Sql.Format (_T("SELECT   ID, NianName FROM  nyxm_lang_kewai_nian"));
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("NianName"));
        m_comboxKeWaiNianji.AddString(Name);
        pRecdPtr->MoveNext();
    }
    m_comboxKeWaiNianji.SetCurSel(0);
}
void CChuangGuan::InitKeWaiTypeCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    CString ID;
    CString StrOfSelected;

    //banben
    m_comboxKeWaiNianji.GetLBText(m_comboxKeWaiNianji.GetCurSel(),StrOfSelected);
    Sql.Format(_T("SELECT   ID, NianName FROM  nyxm_lang_kewai_nian where NianName=\"%s\""),StrOfSelected);
    ID=theApp.GetSingleFieldEx(Sql,false);
    if (ID.IsEmpty())
    {
        return;
    }
    m_comboxKeWaiType.ResetContent();
    Sql.Format(_T("SELECT   ID, nianji_id, type_name FROM nyxm_lang_kewai_type where nianji_id=%d"),_ttoi(ID));
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("type_name"));
        m_comboxKeWaiType.AddString(Name);
        pRecdPtr->MoveNext();
    }
    m_comboxKeWaiType.SetCurSel(0);
}
void CChuangGuan::InitKeWaiWenCmb()
{
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString Name;
    CString ID;
    CString StrOfSelected;

    //banben
    m_comboxKeWaiType.GetLBText(m_comboxKeWaiType.GetCurSel(),StrOfSelected);
    Sql.Format(_T("SELECT   ID, nianji_id, type_name FROM nyxm_lang_kewai_type where type_name=\"%s\""),StrOfSelected);
    ID=theApp.GetSingleFieldEx(Sql,false);
    if (ID.IsEmpty())
    {
        return;
    }
    m_comboxKeWaiWen.ResetContent();
    Sql.Format(_T("SELECT   nianji_id, type_id, ID, wen_name, wen_cont, wen_mp3, wen_mm FROM  nyxm_lang_kewai_wen where type_id=%d"),_ttoi(ID));
    pRecdPtr = theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }
    while(!pRecdPtr->GetadoEOF())
    {
        Name=GetCollect(pRecdPtr,_T("wen_name"));
        m_comboxKeWaiWen.AddString(Name);
        pRecdPtr->MoveNext();
    }
    m_comboxKeWaiWen.SetCurSel(0);
    IniKeWaiKeWenNeiRongCmb();
}
void CChuangGuan::IniKeWaiKeWenNeiRongCmb()
{
    //清空richedit
    //m_edit.SetSel(0,-1);
    //m_edit.Clear();
    strAllNeiRong.Format(_T(""));
    m_edit.SetWindowText(_T(""));
    //从数据库中查询的数据集
    _RecordsetPtr pRecdPtr;
    //Sql
    CString Sql;
    //combox中的元素
    CString strOfFenju;
    //上一级表ID及combox选项
    //[年纪->版本->单元->课文->内容]
    CString ID;
    CString StrOfSelected;

    //内容IDC_RICHEDIT21，从分句中查询
    m_comboxKeWaiWen.GetLBText(m_comboxKeWaiWen.GetCurSel(),StrOfSelected);
    //文章标题
    m_kewenName.SetWindowTextW(StrOfSelected);
    Sql.Format(_T("SELECT ID FROM  nyxm_lang_kewai_wen where wen_name=\"%s\""),StrOfSelected);
    ID=theApp.GetSingleFieldEx(Sql,false);
    if (ID.IsEmpty())
    {
        return;
    }

    Sql.Format(_T("SELECT   nianji_id, type_id, wen_id, wen_fen_id, fenju, mp3 FROM      nyxm_lang_kewai_con_fen where wen_id=%d"),_ttoi(ID));
    pRecdPtr= theApp.GetTablesBySql(Sql,false);	
    if (pRecdPtr == NULL)
    {
        return;
    }

    //SetColor();
    //m_edit.SetSelectionCharFormat(m_cfDefault); 
    SetUnderline(TRUE);
    GetDlgItem(IDC_RICHEDIT21)->SetFont(&m_editFont);
    /*SetSizeOfFont();*/
    while(!pRecdPtr->GetadoEOF())
    {
        strOfFenju=GetCollect(pRecdPtr,_T("fenju"));
        strOfFenju.Remove('\r');
        strOfFenju.Remove('\n');
        strOfFenju.Remove('\t');
        if ((-1) != strOfFenju.Find(_T(",")) || (-1) != strOfFenju.Find(_T(".")) || (-1) != strOfFenju.Find(_T("!")) ||(-1) != strOfFenju.Find(_T("?")))
        {
            /*strOfFenju = strOfFenju.Left(strOfFenju.GetLength() - 1);*/
            strOfFenju+=_T("\r");
        }		
        m_edit.SetSel( -1,-1);
        m_edit.ReplaceSel( (LPCTSTR)strOfFenju );
        strAllNeiRong+=strOfFenju;
        pRecdPtr->MoveNext();
    }
    
    pRecdPtr->Close();
}
void CChuangGuan::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CString strInfo;
    int iLength = 0;

    switch (nIDEvent)
    {
    case TIMER_BEFORE_AUDIO_RECORD:
        KillTimer(TIMER_BEFORE_AUDIO_RECORD);
        GetDlgItem(IDC_EDIT1_LUYIN_TISHI)->ShowWindow(TRUE);

        //theApp.PlayDingMusic();

        //m_iMaxProcessRange = m_edit.GetLineCount() + 1;
        //MyAfxMessageBox(_T(" m_iMaxProcessRange :%d"), m_iMaxProcessRange);
        m_iCurrentProcess = 0;
        m_Progress.SetRange(0, m_iMaxProcessRange);
        m_Progress.SetStep(1);
        m_Progress.SetPos(0);
		nstart = 0;
        SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, _T("录音开始......"));
        RefreshStaticText(IDC_EDIT1_LUYIN_TISHI);
        //录音
        theApp.SpeechRecognition_Start();
        SetTimer(TIMER_AUDIO_RECORD, 1000, NULL);
        break;

    case TIMER_AUDIO_RECORD:
        {
            KillTimer(TIMER_AUDIO_RECORD);
            SetColor();
#if 0
            nstart=m_edit.LineIndex(m_iCurrentProcess);
            nEnd=m_edit.LineLength(m_iCurrentProcess);
#endif

			for(int idx = nstart; idx < strAllNeiRong.GetLength(); idx++){
				if(strAllNeiRong[idx] == '.' || strAllNeiRong[idx] == '!' || strAllNeiRong[idx] == '?'){
					nEnd = idx + 1;
					break;
				}
			}

            ((CEdit*)GetDlgItem(IDC_RICHEDIT21))->SetSel(nstart, nEnd, TRUE);
			nstart = nEnd;
            m_edit.SetSelectionCharFormat(m_cfDefault);
            CString strSent = m_edit.GetSelText();
            strSent.Trim();
            if (!strSent.IsEmpty())
            {
                theApp.PlayDingMusic();
            }

            if (m_iCurrentProcess++ == m_iMaxProcessRange)
            {
				m_Progress.StepIt();
                ((CEdit*)GetDlgItem(IDC_RICHEDIT21))->SetSel(-1, -1, true);

                SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, _T("录音结束......"));
                RefreshStaticText(IDC_EDIT1_LUYIN_TISHI);

                //结束录音
                theApp.SpeechRecognition_Stop(theApp.m_strCommonWaveFile);

                //开始评分的进度条等相关处理
                SetTimer(TIMER_MARK_SENTENCE_PROGRESS, 1000, NULL);
                m_iMaxProcessRange = 10;
                m_iCurrentProcess = 0;
                m_Progress.SetRange(0, m_iMaxProcessRange);
                m_Progress.SetStep(1);
                m_Progress.SetPos(0);
                m_Progress.ShowWindow(TRUE);
                SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, _T("发音评分中......"));
                /*RefreshStaticText(IDC_EDIT1_LUYIN_TISHI);*/

                CString MsgInfo;
                /*GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);*/
                /*theApp.SpeechRecognitionCompare2(m_strCurrentReadInfo, MsgInfo, CTeacherManagerApp::ct_sent );*/

                theApp.SpeechRecognitionCompare2(strAllNeiRong, MsgInfo, CTeacherManagerApp::ct_sent );
            }
            else 
            {
				m_Progress.StepIt();
				SetTimer(TIMER_AUDIO_RECORD, GetPauseMSecond(strSent.GetLength()), NULL);
            }
        }

        break;
    case TIMER_MARK_SENTENCE_PROGRESS:
        //Sleep(1000);
        m_iCurrentProcess++;
        m_Progress.StepIt();

        if (m_iCurrentProcess == m_iMaxProcessRange)
        {
            KillTimer(TIMER_MARK_SENTENCE_PROGRESS);

            SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, _T("发音评分结束......"));
            RefreshStaticText(IDC_EDIT1_LUYIN_TISHI);
            CString strBadWord;
            if (theApp.m_strRecognitionCompare[7].IsEmpty())
            {
                strBadWord.Format(_T("整体得分：%s"), theApp.m_strRecognitionCompare[8].GetString());
            }
            else
            {
                if (theApp.m_strRecognitionCompare[8].CompareNoCase(_T("0")))
                {
                    strBadWord.Format(_T("整体得分：%s"),  theApp.m_strRecognitionCompare[8].GetString());
                    ReWriteRichEditInfoByJudgeResult(strBadWord);

                }
                else
                {
                    strBadWord.Format(_T("非常遗憾，整体得分:0分"));
                }

            }

            SetDlgItemText(IDC_EDIT1_LUYIN_TISHI, strBadWord.GetString());
            RefreshStaticText(IDC_EDIT1_LUYIN_TISHI);

            //如果在 theApp.m_strRecognitionCompare[7]  中可以找到，就显示为红色
        }
        break;
    default:
        strInfo.Format(_T("无效的定时器Id:%d"), nIDEvent);
        MessageBoxW(strInfo, _T("提示"), 0);
        break;
    }

    CDialogEx::OnTimer(nIDEvent);
}
void CChuangGuan::RefreshStaticText(const int iStaticIndex)
{
    CRect rc;
    GetDlgItem(iStaticIndex)->GetWindowRect(&rc);
    //转换为相对坐标
    ScreenToClient(&rc);
    //刷新指定区域,注意第2个参数为真,即刷新背景
    InvalidateRect(&rc,TRUE);
}
void CChuangGuan::ReWriteRichEditInfoByJudgeResult(CString &strBadWord)
{
    std::vector<CString> WordVec;
    CString Tmp = m_strCurrentReadInfo;
    /*CString Tmp=strAllNeiRong;	*/
    //没有要加强的单词直接返回。
    if (theApp.m_strRecognitionCompare[7].IsEmpty())
    {
        return;
    }
    strBadWord+=_T("\r\n您需要加强的单词有:");
    int iPos = 0;
    CString strFuHao;
    do 
    {
        iPos = Tmp.Find(_T(" "));
        CString SubStr = Tmp.Left(iPos);
        Tmp = Tmp.Right(Tmp.GetLength() - iPos - 1);

        if ((-1) != SubStr.Find(_T(",")) || (-1) != SubStr.Find(_T(".")) || (-1) != SubStr.Find(_T("!")) ||(-1) != SubStr.Find(_T("?")))
        {
            strFuHao = SubStr.Right(1);
            SubStr = SubStr.Left(SubStr.GetLength() - 1);
        }
        WordVec.push_back(SubStr.MakeLower());
        if (!strFuHao.IsEmpty())
        {
            WordVec.push_back(strFuHao);
            strFuHao.Empty();
        }
    }while((-1) != iPos);


    if ((-1) != Tmp.Find(_T(",")) || (-1) != Tmp.Find(_T(".")) || (-1) != Tmp.Find(_T("!")) ||(-1) != Tmp.Find(_T("?")))
    {
        strFuHao = Tmp.Right(1);
        Tmp = Tmp.Left(Tmp.GetLength() - 1);
    }

    if (!strFuHao.IsEmpty())
    {
        WordVec.push_back(Tmp.MakeLower());
        WordVec.push_back(strFuHao);
    }

    theApp.CleanAllWordInRichEdit(m_edit);
    auto Iter = WordVec.begin();
    for (;Iter != WordVec.end();++Iter)
    {
        CString strWord = *Iter;
        if ((-1) != strWord.Find(_T(",")) || (-1) != strWord.Find(_T(".")) || (-1) != strWord.Find(_T("!")) ||(-1) != strWord.Find(_T("?"))||(-1)!=strWord.Find(_T("'")))
        {
            ;
        }
        else
        {
            strWord+=_T(" ");
        }
        if ((-1) == theApp.m_strRecognitionCompare[7].Find(*Iter))
        {
            theApp.WriteCommonWordToRichEdit(m_edit, strWord);			
        }
        else
        {
            theApp.WriteCommonWordToRichEdit(m_edit, strWord, TRUE);
            strBadWord+=strWord;
        }
    }
}
void CChuangGuan::AudioRecord()
{	
    KillTimer(TIMER_BEFORE_AUDIO_RECORD);
    KillTimer(TIMER_AUDIO_RECORD);
    KillTimer(TIMER_MARK_SENTENCE_PROGRESS);
    GetDlgItem(IDC_EDIT1_LUYIN_TISHI)->ShowWindow(FALSE);
    m_Progress.ShowWindow(TRUE);
    SetTimer(TIMER_BEFORE_AUDIO_RECORD, 1000, NULL);	
}

void CChuangGuan::OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: 在此添加控件通知处理程序代码
    m_Progress.SetRange(1,100); 
    for(int i=0; i<100; i++) 
    { 
        m_Progress.StepIt(); 
        Sleep(10); 
    } 
    *pResult = 0;
}

int CChuangGuan::GetPauseMSecond(int nTextLen)
{
    int iSecond = (nTextLen / 25);
    if (iSecond == 0)
    {
        iSecond = 1;
    }

    iSecond = (iSecond*2 + 1)*1300;

    return iSecond;
}

void CChuangGuan::OnCbnSelchangeComboClass()
{
    // TODO: 在此添加控件通知处理程序代码
    CString StrOfSelected;
    m_comboxNianji.GetLBText(m_comboxNianji.GetCurSel(),StrOfSelected);
    InitBanBenCmb();
    InitDanYuanCmb();
    IniKeWenCmb();
    IniKeWenNeiRongCmb();
}

void CChuangGuan::OnCbnSelchangeComboChuBan()
{
    // TODO: 在此添加控件通知处理程序代码
    InitDanYuanCmb();
    IniKeWenCmb();
    IniKeWenNeiRongCmb();
}


void CChuangGuan::OnCbnSelchangeComboFenLei()
{
    // TODO: 在此添加控件通知处理程序代码
    IniKeWenCmb();
    IniKeWenNeiRongCmb();
}


void CChuangGuan::OnCbnSelchangeComboKeWenName()
{
    // TODO: 在此添加控件通知处理程序代码
    IniKeWenNeiRongCmb();
}





void CChuangGuan::OnCbnSelchangeComboClassB()
{
    InitKeWaiTypeCmb();
    InitKeWaiWenCmb();
}


void CChuangGuan::OnCbnSelchangeComboFenLeiB()
{
    InitKeWaiWenCmb();
}


void CChuangGuan::OnClickedRadioChoiceText()
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);

    //int nStatus = m_radioChoiceText.GetCheck();
    switch (m_nRadioId)
    {
    case 0:
        OnBnClickedButtonKeWen();
        break;

    case 1:
        OnBnClickedButtonKeWai();
        break;
    default:
        break;
    }
}


void CChuangGuan::OnCbnSelchangeComboKeWenNameB()
{
    // TODO: Add your control notification handler code here

    m_indexCurrent = m_comboxKeWaiWen.GetCurSel();
    IniKeWaiKeWenNeiRongCmb();
    GetDlgItemText(IDC_RICHEDIT21, m_strCurrentReadInfo);
    //AudioRecord();
}
